﻿Imports System.Net
Imports System.IO

Public Class Form1
    Dim tCount As Integer = 0
    Dim ref(100) As Threading.Thread
    Dim taskon As Boolean = False
    Public Const ThreadTotal = 50
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim url As String = TextBox1.Text
        If url = "" Then Exit Sub
        If taskon = True Then Exit Sub
        For i As Int16 = 0 To ThreadTotal
            ref(i) = New Threading.Thread(AddressOf getHtmlStr)
            ref(i).Start(url)
        Next
        taskon = True
    End Sub
    Private Function getHtmlStr(ByVal strURL As String) As String  '获取源码
        Do While 1 = 1
            On Error Resume Next
            Dim httpReq As System.Net.HttpWebRequest
            Dim httpResp As System.Net.HttpWebResponse
            Dim httpURL As New System.Uri(strURL)
            httpReq = CType(WebRequest.Create(httpURL), HttpWebRequest)
            httpReq.Method = "GET"
            httpResp = CType(httpReq.GetResponse(), HttpWebResponse)
            'Dim reader As StreamReader = New StreamReader(httpResp.GetResponseStream, System.Text.Encoding.GetEncoding("GB2312"))
            'getHtmlStr = reader.ReadToEnd
            'reader.Close()
            httpResp.Close()
            tCount += 1
            Invoke(New vShowTimes(AddressOf ShowTimes))
        Loop
    End Function
    Sub ShowTimes()
        Label1.Text = tCount.ToString
    End Sub
    Delegate Sub vShowTimes()
    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        If taskon = True Then
            For i As Int16 = 0 To ThreadTotal
                ref(i).Abort()
            Next
            taskon = False
        End If
        tCount = 0
        Label1.Text = "等待命令"
        Application.DoEvents()
    End Sub
End Class
